package com.liyingtao.servlet;

import com.liyingtao.bean.Admin;
import com.liyingtao.biz.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet("/registerServlet")
public class registerServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username=request.getParameter("username");
        String pwd=request.getParameter("pwd");

        Admin admin=new Admin(username,pwd);

        AdminService adminservice = new AdminService();
        int flag=adminservice.addAdmin(admin);
        PrintWriter out = response.getWriter();
        if(flag>0){
            response.setContentType("text/html; charset=UTF-8");
            out.print("<script>alert('注册成功'); location.href='login.html'</script>");
        }else{
            out.print("<script>alert('注册失败');'</script>");
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
